//
//  MainTabBar.m
//  TooToo
//  底部导航    莫浩
//  Created by 莫浩 on 13-7-23.
//  Copyright (c) 2013年 appfactory. All rights reserved.
//

#import "MainTabBar.h"
#import "UIImage+GIF.h"
#import <QuartzCore/QuartzCore.h>

@interface MainTabBar()
{
    NSMutableArray * btnArray;
    NSMutableArray * imgArray;
    
    NSArray * animationImgArray;
    NSArray * picArr;

    MainTabBarItem _currentSelected;
}

@end

@implementation MainTabBar


@synthesize currentSelected = _currentSelected;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowRadius = 2;
        self.layer.shadowOpacity = 0.1;
        self.layer.shadowOffset = CGSizeMake(0, 0);
        
        UIToolbar * bgView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        bgView.barStyle = UIBarStyleDefault;
        [bgView setShadowImage:[UIImage new] forToolbarPosition:UIBarPositionTop];
        bgView.clipsToBounds = YES;
        [bgView layoutIfNeeded];
        [self addSubview:bgView];
        
        btnArray = [NSMutableArray array];
        imgArray = [NSMutableArray array];
        //未选中的图片数组
        picArr = @[[UIImage imageNamed:@"tbar_home"],
                   [UIImage imageNamed:@"tbar_my"]];
        //gif图数组  选中
        animationImgArray = @[[UIImage imageNamed:@"tbar_home_selected"],
                              [UIImage imageNamed:@"tbar_my_selected"],
                              ];
        
        NSArray * btnTitleArr = @[NSLocalizedString(@"tBarHome",nil),NSLocalizedString(@"tBarMy",nil)];
        
        for (int i = 0; i < btnTitleArr.count; i++) {
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.tag = i+MainTabBarItemHome;
            btn.frame = CGRectMake(IPHONEWIDTH/btnTitleArr.count * i, 0, IPHONEWIDTH/btnTitleArr.count, 50);
            [btn addTarget:self action:@selector(itemSelected:) forControlEvents:UIControlEventTouchUpInside];
            btn.userInteractionEnabled = YES;
            [bgView addSubview:btn];
            [btn setExclusiveTouch:YES];
            [btnArray addObject:btn];
            
            UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i == 0 ? 90*UISCALE : (btn.width - 90*UISCALE - 25), 5, 25, 25)];
            imageView.backgroundColor = [UIColor clearColor];
            imageView.image = picArr[i];
            imageView.contentMode =  UIViewContentModeScaleAspectFill;
            [btn addSubview:imageView];
            [imgArray addObject:imageView];
            
            UILabel *title = [UILabel makeLabel:^(TTLabelExtend *make) {
                make.tag = i+50;
                make.addTextColor(FBEE_COLOR_UNTOUCH).addText(btnTitleArr[i]).addFont([UIFont systemFontOfSize:11.0f]);
                [make sizeToFit];
                make.centerX = imageView.centerX;
                make.top = imageView.bottom + 5;
            }];
            [btn addSubview:title];
            
        }

        
    }
    return self;
}

-(void)setCurrentSelected:(MainTabBarItem)currentSelected
{
    
    if (_currentSelected == currentSelected) {
        return;
    }
    
    for (int i = 0; i<btnArray.count; i++){
    
        UIButton * btn = btnArray[i];
        UIImageView *imgView = imgArray[i];
        UILabel *title = [btn viewWithTag:i+50];
        
        if (btn.tag == currentSelected) {
            
            btn.selected = YES;
            imgView.image = animationImgArray[i];
            title.textColor = FBEE_COLOR_THEME;

        }else{
            btn.selected = NO;
            imgView.image = picArr[i];
            title.textColor = FBEE_COLOR_UNTOUCH;
        }
    }
    
}

-(void)itemSelected:(UIButton *)sender
{
    self.userInteractionEnabled = NO;

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        self.userInteractionEnabled = YES;
    });
    
    UIButton *item = sender;
    
    if ([self.delegate respondsToSelector:@selector(mainTabBar:selectItem:)]) {
        [self.delegate mainTabBar:self selectItem:item.tag];
    }

}

@end
